//
//  ST_BTStaturePickerView.swift
//  btvideo
//
//  Created by mac on 2020/5/30.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_BTStaturePickerView: UIView {

    static let height = 250+BTSafeAreaBottom
    
    
    static let statureOptions: [Int] = Array(130...200)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = Color.init(hexString: "#F2F4F7")
        
        self.addSubview(top_view)
        top_view.snp.makeConstraints({ (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(44)
        })
        
        self.addSubview(confirm_picker_btn)
        confirm_picker_btn.snp.makeConstraints({ (make) in
            make.right.equalTo(self).offset(-50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints({ (make) in
            make.centerY.equalTo(confirm_picker_btn)
            make.centerX.equalToSuperview()
        })
        
        self.addSubview(cancel_picker_btn)
        cancel_picker_btn.snp.makeConstraints({ (make) in
            make.left.equalTo(self).offset(50)
            make.width.height.equalTo(44)
            make.top.equalTo(self)
        })
        
        self.addSubview(pickerView)
        pickerView.snp.makeConstraints({ (make) in
            make.top.equalTo(self.cancel_picker_btn.snp.bottom).offset(10)
            make.left.right.equalTo(self)
            make.bottom.equalTo(self).offset(-BTSafeAreaBottom)
        })
        
        let line = UIView()
        self.addSubview(line)
        line.backgroundColor = Color.init(hexString: "#E7ECF4")
        line.snp.makeConstraints({ (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(1)
        })
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    var pickerFont = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.medium)
    
    lazy var top_view:UIView = {
       let control = UIView()
        control.backgroundColor = Color.white
        return control
    }()
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "请选择"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    lazy var confirm_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("完成")
        control.setTitleColorForAllStates(Color.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(confirm_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    lazy var cancel_picker_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("取消")
        control.setTitleColorForAllStates(Color.init(hexString: "#999999")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(cancel_picker_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    

    
    lazy var pickerView:UIPickerView = {
        let control = UIPickerView()
        control.delegate = self
        control.dataSource = self
        return control
    }()
    
    var normalSelectedIndexPath:IndexPath?
    
    fileprivate var selectedIndexPath = IndexPath.init(row: 0, section: 0)
    
//    MARK: - Event
    
    func scrollTo(indexPath:IndexPath){
        
        selectedIndexPath = indexPath
        
        self.pickerView.selectRow(indexPath.section, inComponent: 0, animated: true)
        
        self.pickerView.selectRow(indexPath.row, inComponent: 1, animated: true)
     
    }
    
    
    var confirm_pick_closure:((_ title:String,_ subTitle:String,_ indexPath:IndexPath)->())?
    
    @objc func confirm_picker_action(){
        
        if let title = ST_BTStaturePickerView.statureOptions[safe:selectedIndexPath.section]?.string,let childTitle = ST_BTStaturePickerView.statureOptions[safe:selectedIndexPath.row]?.string{
            
            confirm_pick_closure?(title,childTitle,selectedIndexPath)
        }
        
        
        cancel_picker_action()
    }
    
    
    @objc func cancel_picker_action(){
        
        self.isHidden = true
        
    }
    
}

extension ST_BTStaturePickerView:UIPickerViewDelegate,UIPickerViewDataSource{
    
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }

    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        return ST_BTStaturePickerView.statureOptions.count
    }
    
    /**
     暗黑模式bug
     */
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//
//        return ST_BTStaturePickerView.statureOptions[row].string + "cm"
//    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        
       let label = (view as? UILabel) ?? UILabel()

       label.textColor = .black
       label.textAlignment = .center
       label.font = self.pickerFont

       // where data is an Array of String
       label.text = ST_BTStaturePickerView.statureOptions[row].string + "cm"

       return label
     }
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        
        
        if component == 0{
            selectedIndexPath = IndexPath.init(row: selectedIndexPath.row, section: row)
            pickerView.reloadComponent(1)
        }else{
            selectedIndexPath = IndexPath.init(row: row, section: selectedIndexPath.section)
        }
        
        
        
    }
    
}
